<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>构造函数修改复习</title>
</head>
<body>
    
</body>
</html>
<script>
    /*
        创建一个Person构造函数
    */

    function Person(name,age,gender){
        this.name = name;
        this.age = age;
        this.gender = gender;
        //向对象中添加一个方法
        this.sayName = xyj; 
    }

    // 将sayName方法在全局作用域中定义
    /*
        将函数定义在全局作用域，污染了全局作用域的命名空间
            而且定义在全局作用域中也很不安全
    */
    function xyj(){
            alert(this.name);
    }

    // 实例化 Person 
    var per = new Person("孙悟空",18,"男");
    var per2 = new Person("猪八戒",20,"男");
    var per3 = new Person("沙和尚",30,"男");
    per.sayName();
    per2.sayName();
    per3.sayName();

    console.log(per.name);
    console.log(per2.name);
    console.log(per3.name);

</script>